/******************************************************************************
 ** auth: liukai
 ** date: 2017/7
 ** ver : 1.0
 ** desc:  用户相关的接口
 ** Copyright © 2017年 尧尚信息科技(wwww.yourshares.cn). All rights reserved
 ******************************************************************************/
//  Roger:18684909663


import Moya
import YSKit

public enum UpdateType: Int{
    case nickName = 1
    case age = 2
    case sex = 3
}


public enum CommentHistroyType: Int{
    case help = 1
    case seek = 2
}

public enum UserAPI: YSTargetRequestable {
    
    case signIn(username: String, password: String)
    case registe(phone: String, code: String,password: String, token: String)
    case getCode(phone: String)
    case forgetPwd(phone: String, code: String,password: String)
    case userInfo(token: String)
    case updateUserInfo(token: String, value: String, type: UpdateType)
    case updateUserHeader(token: String, phone: String, imageData: UIImage)
    case updateLocation(token: String, latitude: Double, longitude: Double)
    case getPointHistory(token: String)
    case goodsDetail(token: String, goodId: String)
    case updatePushToken(pushToken :String, token: String)
    case sendPushMessage(message: String, deviceToken: String)
    case sendFeedbackConment(token: String, conment: String)
    case getNearHelperAction(token: String, latitude: Double, longitude: Double)
    
    case nearbyUsers(token: String, latitude: Double, longitude: Double)
    case nearbyAskForHelpers(token: String, latitude: Double, longitude: Double)
    case nearbyAcceptHelpers(token: String, latitude: Double, longitude: Double)

    case getStatus(token: String)
    case getPointMainAciton(token: String)
    case buyGoodsAction(token: String, goodId: String, receiverName: String, receiverAddress: String, receiverPhone: String)
    case helpHistoryAction(token: String)
    case seekHelpHistoryAction(token: String)
    case commentHistroyAction(token: String,type:CommentHistroyType,pageIndex:String,pageCount:String )
    case helpInfo(token: String, helpid: String)

    /// 请求的URL分别写在API内部
    fileprivate struct RequestURL {
        // MARK: Market
        static let signIn = "/u4uapi/Index/LoginAction"
        static let registe = "/u4uapi/Index/RegisteAction"
        static let getCode = "/u4uapi/Index/GetCodeAction"
        static let forgetPwd = "/u4uapi/Index/ForgetPwdAction"
        static let userInfo = "/u4uapi/Index/GetUserInfoAction"
        static let updateUserInfoAction = "/u4uapi/Index/UpdateUserInfoAction"
        static let updateAvator = "/u4uapi/Index/UpdateAvatorAction"

        static let pointHistory = "/u4uapi/Index/PointHistoryAction"
        static let goodsDetail = "/u4uapi/Index/GoodsDetailAction"
        
        static let updatePushToken = "/u4uapi/Index/UpdateDeviceTokenAction"
        static let updateLocation = "/u4uapi/Index/UpdateMyLocationAction"
        static let sendPushMessage = "/u4uapi/Index/pushmsg"
        static let sendFeedbackConment = "/u4uapi/Index/FeedbackAction"
        static let getNearHelperAction = "/u4uapi/Index/GetNearHelperAction"
        static let nearbyUsers = "/u4uapi/Index/NearbyUsers"
        static let nearbyAcceptHelpers = "/u4uapi/Index/NearbyAcceptHelpers"
        static let nearbyAskForHelpers = "/u4uapi/Index/NearbyAskForHelpers"

        
        static let getStatus = "/u4uapi/Index/UserStatusAction"
        static let getPointMainAciton = "/u4uapi/Index/PointMainAciton"
        static let buyGoodsAction = "/u4uapi/Index/BuyGoodsAction"
        static let helpHistoryAction = "/u4uapi/Index/HelpHistoryAction"
        static let seekHelpHistoryAction = "/u4uapi/Index/SeekHelpHistoryAction"
        static let commentHistroyAction = "/u4uapi/Index/CommentHistroyAction"
        static let helpInfo = "/u4uapi/Index/GetStatusByHelpIdAction"

    }

    public var api: YSTargetRequest {
        switch self {
        case .signIn(let username, let password):
            var parameters: [String: Any] = [:]
            parameters["username"] = username
            parameters["password"] = password
            return YSTargetRequest(path: RequestURL.signIn,
                                   method: .post,
                                   parameters: parameters
            )
           
        case .getCode(let phone):
            var parameters: [String: Any] = [:]
            parameters["phone"] = phone
            return YSTargetRequest(path: RequestURL.getCode,
                                   method: .post,
                                   parameters: parameters
            )

        case .registe(let phone, let code, let password, let token):
            var parameters: [String: Any] = [:]
            parameters["phone"] = phone
            parameters["code"] = code
            parameters["password"] = password
            parameters["token"] = token
            return YSTargetRequest(path: RequestURL.registe,
                                   method: .post,
                                   parameters: parameters
            )
        case .forgetPwd(let phone, let code, let password):
            var parameters: [String: Any] = [:]
            parameters["phone"] = phone
            parameters["code"] = code
            parameters["password"] = password
            parameters["token"] = Auth.token
            return YSTargetRequest(path: RequestURL.forgetPwd,
                                   method: .post,
                                   parameters: parameters
            )
        case .userInfo(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            return YSTargetRequest(path: RequestURL.userInfo,
                                   method: .post,
                                   parameters: parameters
            )
        case .getStatus(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            return YSTargetRequest(path: RequestURL.getStatus,
                                   method: .post,
                                   parameters: parameters
            )
        case .updateUserInfo(let token, let value, let type):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["value"] = value
            parameters["type"] = type.rawValue
            return YSTargetRequest(path: RequestURL.updateUserInfoAction,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .updateUserHeader(let token, let phone, let image):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["avator"] = phone+".png"
            if let imageData = image.ys.resetSizeToData(500) {
                let formData: [MultipartFormData] = [MultipartFormData(provider: .data(imageData), name: "avator", fileName: parameters["avator"] as? String, mimeType: "image/png")]
                return YSTargetRequest(
                                   path: RequestURL.updateAvator,
                                       method: .post,
                                       parameters: parameters,
                                       baseURL:URL(string: HostManager.kMainHost)!,
                                       task: .upload(UploadType.multipart(formData))
                )
            }else{
                print("图片压缩失败...")
                return YSTargetRequest(path: RequestURL.updateAvator,
                                       method: .post,
                                       parameters: parameters
                )

            }
            
        case .getPointHistory(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            return YSTargetRequest(path: RequestURL.pointHistory,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .goodsDetail(let token, let goodId):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["goodId"] = goodId
            return YSTargetRequest(path: RequestURL.goodsDetail,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .updatePushToken(let pushToken, let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["deviceToken"] = pushToken
            return YSTargetRequest(path: RequestURL.updatePushToken,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .updateLocation(let token, let latitude, let longitude):
            var parameters: [String: Any] = [:]
            parameters["latitude"] = latitude
            parameters["longitude"] = longitude
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.updateLocation,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .sendPushMessage(let message, let deviceToken):
            var parameters: [String: Any] = [:]
            parameters["pushtoken"] = deviceToken
            parameters["message"] = message
            
            return YSTargetRequest(path: RequestURL.sendPushMessage,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .sendFeedbackConment(let token, let conment):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["conment"] = conment
            
            return YSTargetRequest(path: RequestURL.sendPushMessage,
                                   method: .post,
                                   parameters: parameters
            )
            
            
        case .getNearHelperAction(let token, let latitude, let longitude):
            var parameters: [String: Any] = [:]
            parameters["latitude"] = latitude
            parameters["longitude"] = longitude
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.getNearHelperAction,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .nearbyUsers(let token, let latitude, let longitude):
            var parameters: [String: Any] = [:]
            parameters["latitude"] = latitude
            parameters["longitude"] = longitude
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.nearbyUsers,
                                   method: .post,
                                   parameters: parameters
            )
            
            
        case .nearbyAskForHelpers(let token, let latitude, let longitude):
            var parameters: [String: Any] = [:]
            parameters["latitude"] = latitude
            parameters["longitude"] = longitude
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.nearbyAskForHelpers,
                                   method: .post,
                                   parameters: parameters
            )
        case .nearbyAcceptHelpers(let token, let latitude, let longitude):
            var parameters: [String: Any] = [:]
            parameters["latitude"] = latitude
            parameters["longitude"] = longitude
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.nearbyAcceptHelpers,
                                   method: .post,
                                   parameters: parameters
            )
            
            
        case .getPointMainAciton(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.getPointMainAciton,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .buyGoodsAction(let token, let goodId, let receiverName, let receiverAddress, let receiverPhone):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["goodId"] = goodId
            parameters["receiverName"] = receiverName
            parameters["receiverAddress"] = receiverAddress
            parameters["receiverPhone"] = receiverPhone
            
            return YSTargetRequest(path: RequestURL.buyGoodsAction,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .helpHistoryAction(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.helpHistoryAction,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .seekHelpHistoryAction(let token):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            
            return YSTargetRequest(path: RequestURL.seekHelpHistoryAction,
                                   method: .post,
                                   parameters: parameters
            )
            
        case .commentHistroyAction(let token,let type, let pageIndex,let pageCount):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["type"] = type.rawValue
            parameters["pageIndex"] = pageIndex
            parameters["pageCount"] = pageCount
            
            return YSTargetRequest(path: RequestURL.commentHistroyAction,
                                   method: .post,
                                   parameters: parameters
            )
        case .helpInfo(let token, let helpid):
            var parameters: [String: Any] = [:]
            parameters["token"] = token
            parameters["helpid"] = helpid
            return YSTargetRequest(path: RequestURL.helpInfo,
                                   method: .post,
                                   parameters: parameters
            )
            
        }
        
       
    }

    public var path: String {
        return self.api.path
    }
    // 获取基本的url，每次发生地址变更时，替换这个url
    public var baseURL: URL {
        
        return self.api.baseURL ?? URL(string: HostManager.kMainHost)!
    }
    public var method: Moya.Method {
        return self.api.method
    }
    public var parameters: [String: Any]? {
        return self.api.parameters
    }
    public var sampleData: Data {
        return self.api.sampleData
    }
    public var task: Task {
        return self.api.task
    }
    public var validate: Bool {
        return false
    }
    public var parameterEncoding: Moya.ParameterEncoding {
        return self.api.parameterEncoding
    }
}
